﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using SLRTHackathon.DataModels;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;

namespace SLRTHackathon.Views
{
    public partial class WelcomePage : PhoneApplicationPage
    {
        private ObservableCollection<Car> cars = new ObservableCollection<Car>();
        private const string BrandKey = "Brand";
        private const string IdentificationNameKey = "IdentificationName";

        public WelcomePage()
        {
            InitializeComponent();
            this.DataContext = App.ViewModel;
            this.cars = App.ViewModel.AllCars;
            LoadCars();
            LoadNotifications();
        }

        private void LoadCars()
        {
            CarsStackPanel.Children.Clear();
            foreach (Car car in cars)
            {
                Button button = new Button();
                button.Content = car.IdentificationName;
                RoutedEventArgs args = new RoutedEventArgs();
                button.DataContext = car;

                LinearGradientBrush myBrush = new LinearGradientBrush();
                myBrush.EndPoint = new Point(0.6, 1);
                myBrush.StartPoint = new Point(0.4, 0);
                GradientStop gs1 = new GradientStop();
                gs1.Offset = 0;
                gs1.Color = Color.FromArgb(201, 209, 54, 13);
                GradientStop gs2 = new GradientStop();
                gs1.Offset = 1;
                gs1.Color = Color.FromArgb(201, 105, 52, 16);
                myBrush.GradientStops.Add(gs1);
                myBrush.GradientStops.Add(gs2);
                button.Background = myBrush;
                                
                /*<Button.Background>
                            <LinearGradientBrush  EndPoint="0.6,1" StartPoint="0.4,0">
                               <GradientStop Color="#FFD1360D" Offset="0"/>
                               <GradientStop Color="#C9693410" Offset="1"/>
                            </LinearGradientBrush>
                            </Button.Background>*/
                
                button.Click += new RoutedEventHandler(SelectCarButton_Click);
                CarsStackPanel.Children.Add(button);
            }
        }

        private void LoadNotifications()
        {

            foreach (CustomDependency customDependency in App.ViewModel.AllCustomDependencies)
            {
                ViewNotificationsStackPanel.Children.Clear();
                if (customDependency.EndDate.AddDays(-customDependency.NumberOfDaysBeforeEndDate) <= DateTime.Now)
                {
                    StackPanel sp = new StackPanel();
                    sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
                    TextBlock tb = new TextBlock();
                    tb.Text = "Name: ";
                    sp.Children.Add(tb);
                    TextBlock tb2 = new TextBlock();
                    tb.Text =customDependency.Name;
                    sp.Children.Add(tb2);
                    StackPanel sp2 = new StackPanel();
                    sp2.Orientation = System.Windows.Controls.Orientation.Horizontal;
                    TextBlock tb3 = new TextBlock();
                    tb3.Text = "Description: ";
                    sp2.Children.Add(tb3);
                    TextBlock tb4 = new TextBlock();
                    tb4.Text = customDependency.Description;
                    sp2.Children.Add(tb4);

                    ViewNotificationsStackPanel.Children.Add(sp);
                    ViewNotificationsStackPanel.Children.Add(sp2);
                }
            }
            foreach (ExtendedInsurance extendedInsurance in App.ViewModel.AllExtendedInsurances)
            {
                if (extendedInsurance.EndDate.AddDays(-extendedInsurance.NumberOfDaysBeforeEndDate) <= DateTime.Now)
                {
                    StackPanel sp = new StackPanel();
                    sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
                    TextBlock tb = new TextBlock();
                    tb.Text = "Name: ";
                    sp.Children.Add(tb);
                    TextBlock tb2 = new TextBlock();
                    tb.Text = extendedInsurance.Name;
                    sp.Children.Add(tb2);
                    StackPanel sp2 = new StackPanel();
                    sp2.Orientation = System.Windows.Controls.Orientation.Horizontal;
                    TextBlock tb3 = new TextBlock();
                    tb3.Text = "Description: ";
                    sp2.Children.Add(tb3);
                    TextBlock tb4 = new TextBlock();
                    tb4.Text = extendedInsurance.Description;
                    sp2.Children.Add(tb4);

                    ViewNotificationsStackPanel.Children.Add(sp);
                    ViewNotificationsStackPanel.Children.Add(sp2);
                }
            }
            foreach (Insurance insurance in App.ViewModel.AllInsurances)
            {
                if (insurance.EndDate.AddDays(-insurance.NumberOfDaysBeforeEndDate) <= DateTime.Now)
                {
                    StackPanel sp = new StackPanel();
                    sp.Orientation = System.Windows.Controls.Orientation.Horizontal;
                    TextBlock tb = new TextBlock();
                    tb.Text = "Name: ";
                    sp.Children.Add(tb);
                    TextBlock tb2 = new TextBlock();
                    tb.Text = insurance.Name;
                    sp.Children.Add(tb2);
                    StackPanel sp2 = new StackPanel();
                    sp2.Orientation = System.Windows.Controls.Orientation.Horizontal;
                    TextBlock tb3 = new TextBlock();
                    tb3.Text = "Description: ";
                    sp2.Children.Add(tb3);
                    TextBlock tb4 = new TextBlock();
                    tb4.Text = insurance.Description;
                    sp2.Children.Add(tb4);

                    ViewNotificationsStackPanel.Children.Add(sp);
                    ViewNotificationsStackPanel.Children.Add(sp2);
                }
            }
        }

        void SelectCarButton_Click(object sender, RoutedEventArgs e)
        {
            CarHelperPage.CurrentCar = ((sender as Button).DataContext as Car);
            NavigationService.Navigate(new Uri("/Views/CarHelperPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void AddCarButton_Click(object sender, RoutedEventArgs e)
        {
            //Confirm there is some text in the text box.
            if (NameTextBlock.Text.Length > 0)
            {
                //Create a new to-do item.
                                
                Car newCar = new Car
                {
                    IdentificationName = NameTextBox.Text,
                    Model = ModelTextBox.Text,
                    Brand = BrandTextBox.Text,
                    YearOfFabrication = Int32.Parse(YearOfFabricationTextBox.Text),
                    CID = Double.Parse(CIDTextBox.Text),
                    CarImage = ImageTextBox.Text,
                    LocalVignetteEndDate = VignetteEndDateDatePicker.Value.Value,
                    NumberOfDaysBeforeEndDate = Int32.Parse(DaysBeforeEndDateTextBox.Text)
                };

                //Add the item to the ViewModel.
                App.ViewModel.AddCar(newCar);
                CarHelperPanorama.DefaultItem = CarHelperPanorama.Items[0];
                NavigationService.Navigate(new Uri("/Views/WelcomePage.xaml", UriKind.RelativeOrAbsolute));
                LoadCars();
            }
        }

        private void GoToAddButton_Click(object sender, RoutedEventArgs e)
        {
            CarHelperPanorama.DefaultItem = CarHelperPanorama.Items[1];
            NavigationService.Navigate(new Uri("/Views/WelcomePage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void SettingsBtn_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/SettingsPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void CarDockButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/CarDockPage.xaml", UriKind.RelativeOrAbsolute));
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {

            if (true == PhoneApplicationService.Current.State.ContainsKey(IdentificationNameKey))
            {

                //clear prev value

                PhoneApplicationService.Current.State.Remove(IdentificationNameKey);

            }

            PhoneApplicationService.Current.State.Add(IdentificationNameKey, this.NameTextBox.Text);

            if (true == PhoneApplicationService.Current.State.ContainsKey(BrandKey))
            {

                //clear prev value

                PhoneApplicationService.Current.State.Remove(BrandKey);

            }

            PhoneApplicationService.Current.State.Add(BrandKey, this.BrandTextBox.Text);

        }


        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {



            if (true == PhoneApplicationService.Current.State.ContainsKey(IdentificationNameKey))
            {

                string s = PhoneApplicationService.Current.State[IdentificationNameKey] as string;

                if (!String.IsNullOrEmpty(s))
                {


                    this.NameTextBox.Text = s;

                }

            }



            if (true == PhoneApplicationService.Current.State.ContainsKey(BrandKey))
            {

                string s = PhoneApplicationService.Current.State[BrandKey] as string;

                if (!String.IsNullOrEmpty(s))
                {

                    this.BrandTextBox.Text = s;

                }

            }

        }
    }
}
